package com.cetcs.kmga.dataManager.controller;

import com.cetcs.kmga.common.BaseController;
import com.cetcs.kmga.common.BaseResponse;
import com.cetcs.kmga.common.Constant;
import com.cetcs.kmga.dataManager.annotation.LogAnnotation;
import com.cetcs.kmga.dataManager.entity.global.CodeType;
import com.cetcs.kmga.dataManager.entity.global.LogLevelEnum;
import com.cetcs.kmga.dataManager.entity.global.LogTypeEnum;
import com.cetcs.kmga.dataManager.entity.global.vi.CodeTypeIn;
import com.cetcs.kmga.dataManager.service.feature.FeatureCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 码表的控制层
 *
 * @author xutao
 * @version V1.0 创建时间：2017-09-26 10:24
 *          Copyright 2017 by CETC
 */
@RestController
@RequestMapping("/featureCode")
public class FeatureCodeController extends BaseController {

    @Autowired
    private FeatureCodeService featureCodeService;

    /**
     * 编码类型查询所有编码的值
     *
     * @param typeId
     * @return
     */
    @LogAnnotation(module = "系统管理", logType = LogTypeEnum.业务日志, description = "编码类型查询所有编码的值", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/findFeatureCodesByTypeId", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public BaseResponse findFeatureCodesByTypeId(@RequestParam(value = "typeId", required = false) Integer typeId) {
        try {
            if (typeId == null) {
                return this.error(Constant.SYSTEM_EXCEPTION, "码值类型不能为空");
            }
            return this.ok(featureCodeService.findFeatureCodesByTypeId(typeId));
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }



    /**
     * 编码类型查询所有编码的值
     *
     * @return
     */
    @LogAnnotation(module = "系统管理", logType = LogTypeEnum.业务日志, description = "编码类型查询所有编码的值", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/findFeatureCodesByTypeId/{id}", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public BaseResponse findFeatureCodesById(@PathVariable(value = "id", required = false) Integer id) {
        try {
            return this.ok(featureCodeService.findFeatureCodesByTypeId(id));
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }

    /**
     * 根据码值不同的码值获取不同的码
     *
     * @param typeId 类型ID
     * @param code   编码
     * @param value  值
     * @return
     */
    @LogAnnotation(module = "系统管理", logType = LogTypeEnum.业务日志, description = "根据码值不同的码值获取不同的码", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/findFeatureCodeByIdOrName", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public BaseResponse findFeatureCodeByIdOrName(@RequestParam(value = "typeId", required = false) Integer typeId,
                                                  @RequestParam(value = "code", required = false) String code,
                                                  @RequestParam(value = "value", required = false) String value) {
        try {
            if (typeId == null) {
                return this.error(Constant.SYSTEM_EXCEPTION, "码值类型不能为空");
            }
            return this.ok(featureCodeService.findFeatureCodeByIdOrName(typeId, code, value));
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }

    /**
     * 码值JSON
     *
     * @param typeIns
     * @return
     */
    @LogAnnotation(module = "系统管理", logType = LogTypeEnum.业务日志, description = "码值JSON", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/findFeatureCodesByTypeIds", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    public BaseResponse findFeatureCodesByTypeIds(@RequestBody List<CodeTypeIn> typeIns) {
        try {
            if (typeIns == null) {
                return this.error(Constant.SYSTEM_EXCEPTION, "码值类型不能为空");
            }
            return this.ok(featureCodeService.findFeatureCodesByTypeIds(typeIns));
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }


}
